package com.c7.bm.mybatis.controller;

import com.c7.bm.mybatis.model.C7Activity;
import com.c7.bm.mybatis.model.C7ActivityEmail;
import com.c7.bm.mybatis.model.C7ActivityImages;
import com.c7.bm.mybatis.service.ActivityEmailService;
import com.c7.bm.mybatis.service.ActivityImageQrService;
import com.c7.bm.mybatis.service.ActivityImagesService;
import com.c7.bm.mybatis.service.ActivityService;
import com.c7.bm.mybatis.util.ResponseWrapper;
import com.github.pagehelper.PageInfo;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * c7图片上传类
 *
 * @author <a href="mailto:chenjunwen@xiaomagouche.com">Duke(陈俊文)</a>
 * @create 17/4/12  下午6:44
 */
@Controller
public class ImageController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityImagesService activityImagesService;

    @Autowired
    private ActivityImageQrService activityImageQrService;

    @Autowired
    private ActivityEmailService activityEmailService;


    @RequestMapping(value = "/mobile.html")
    public ModelAndView getAAA(@RequestParam("activityId") Long activityId) {
        return new ModelAndView("redirect:http://www.c7image.com/mobile/index.html?activityId=" + activityId + "&v=" + System.currentTimeMillis());
    }


    @RequestMapping(value = "/WorkChop.html")
    public String getIndex() {
        return "index";
    }


    /**
     * 返回活动列表
     *
     * @return
     */
    @RequestMapping(value = "/bm/getActivityList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper getActivityList() {
        ResponseWrapper responseBean = new ResponseWrapper();
        List<C7Activity> list;
        try {
            list = activityService.getActivityList();
            responseBean.setStatus(0);
            responseBean.addAttribute("dataList", list);
        } catch (Exception e) {
            responseBean.setStatus(1);
            responseBean.setMsg(e.toString());
            return responseBean;
        }
        return responseBean;

    }

    /**
     * 返回活动详情图片
     *
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/bm/getActivityImageList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper getActivityImageList(
            @RequestParam("page") int page,
            @RequestParam("activityId") Long activityId) {
        ResponseWrapper responseBean = new ResponseWrapper();
        PageInfo<C7ActivityImages> list;
        try {
            int rows = 20;
            list = activityImagesService.getActivityImagesList(page, rows, activityId);
            C7Activity activity = activityService.selectByKey(activityId);
            responseBean.setStatus(0);
            responseBean.addAttribute("activity", activity);
            responseBean.addAttribute("dataList", list);
        } catch (Exception e) {
            responseBean.setStatus(1);
            responseBean.setMsg(e.toString());
            return responseBean;
        }
        return responseBean;

    }

    /**
     * 添加活动
     *
     * @return
     */
    @RequestMapping(value = "/bm/addActivity", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper addActivity(@RequestParam("activityName") String activityName) {
        C7Activity activity = new C7Activity();
        ResponseWrapper responseBean = new ResponseWrapper();
        if (activityName == null || activityName == "") {
            activity.setName("新建文件夹");
        } else {
            activity.setName(activityName);
        }
        try {
            activityService.save(activity);
            activity = activityService.generateQrActivity(activity.getId());
            activity.setImageCount(0);
        } catch (Exception e) {
            responseBean.setStatus(1);
            responseBean.setMsg(e.toString());

            return responseBean;
        }
        responseBean.setStatus(0);
        responseBean.addAttribute("activity", activity);
        return responseBean;
    }

    /**
     * 修改活动
     *
     * @return
     */
    @RequestMapping(value = "/bm/editActivity", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper editActivity(@RequestParam("activityId") Long activityId,
                                        @RequestParam("activityName") String activityName) {
        C7Activity activity = activityService.selectByKey(activityId);
        ResponseWrapper responseBean = new ResponseWrapper();
        activity.setName(activityName);
        try {
            activityService.updateNotNull(activity);
            activity.setImageCount(activityImagesService.getAllImagesList(activityId).size());
        } catch (Exception e) {
            responseBean.setStatus(1);
            responseBean.setMsg(e.toString());
            return responseBean;
        }
        responseBean.addAttribute("activity", activity);
        responseBean.setStatus(0);
        return responseBean;
    }


    /**
     * 刪除活動
     *
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/bm/delActivity", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper delActivity(@RequestParam("activityId") Long activityId) {
        activityService.delete(activityId);
        ResponseWrapper responseBean = new ResponseWrapper();
        responseBean.setStatus(0);
        return responseBean;
    }


    /**
     * 刪除活動圖片
     *
     * @param imageId
     * @return
     */
    @RequestMapping(value = "/bm/delActivityImages", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper delActivityImages(@RequestParam("imageId") Long imageId) {
        activityImagesService.delete(imageId);
        ResponseWrapper responseBean = new ResponseWrapper();
        responseBean.setStatus(0);
        return responseBean;
    }


    /**
     * 添加活动图片
     *
     * @param imagesPath
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/bm/uploadImage", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper uploadImage(@RequestParam("imagePath") MultipartFile imagesPath,
                                       @RequestParam("activityId") Long activityId) {
        ResponseWrapper responseWrapper = activityImagesService.uploadImages(imagesPath, activityId);
        return responseWrapper;
    }


    /**
     * 添加logo图片
     *
     * @param imagesPath
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/bm/uploadLogo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper uploadLogo(@RequestParam("logoPath") MultipartFile imagesPath,
                                      @RequestParam("activityId") Long activityId) {
        ResponseWrapper responseWrapper = activityService.uploadLogo(imagesPath, activityId);
        return responseWrapper;
    }

    /**
     * 生成图片悬浮二维码
     *
     * @return
     */
    @RequestMapping(value = "/bm/generateImageQr", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper generateImageQr(@RequestParam("imageId") Long imageId,
                                           @RequestParam("linkValue") String linkValue) {
        return activityImagesService.generateImageQr(imageId, linkValue);
    }

    @RequestMapping(value = "/bm/getImageQr", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper getImageQr(@RequestParam("imageId") Long imageId) {
        return activityImageQrService.getImageQr(imageId);
    }

    @RequestMapping(value = "/bm/uploadQrImg", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public void uploadQrImg(HttpServletResponse response) throws IOException {
        ByteArrayOutputStream out = QRCode.from("Hello World").to(ImageType.PNG).stream();
        FileOutputStream fout = new FileOutputStream(new File(
                "/Users/Duke.chen/Documents/testImg/QR_Code.JPG"));


        fout.write(out.toByteArray());

        fout.flush();
        fout.close();
    }

    /**
     * 保存邮箱地址
     *
     * @return
     */
    @RequestMapping(value = "/bm/saveEmail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseWrapper saveEmail(@RequestParam("activityId") Long activityId,
                                     @RequestParam("activityImageId") Long activityImageId,
                                     @RequestParam("emailAddress") String emailAddress,
                                     @RequestParam("activityName") String activityName) {
        C7ActivityEmail activityEmail = new C7ActivityEmail();
        activityEmail.setActivityId(activityId);
        activityEmail.setEmailAddress(emailAddress);
        activityEmail.setActivityImageId(activityImageId);
        activityEmail.setActivityName(activityName);
        activityEmailService.save(activityEmail);
        ResponseWrapper response = new ResponseWrapper();
        response.setStatus(0);
        return response;
    }

}
